// src/stores/video/config.js
import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
    saveConfig,
    getConfigList,
    setDefaultConfig,
    deleteConfig,
    getDefaultConfig,
    batchDeleteConfigs,
    setCaptionConfig,
    syncKeyAvatars,
    getLastSyncAvatarInfo,
    removeMultiConfig,
    saveMultiConfig,
    fetchMultiAvatarConfig,
    getMultiConfig
} from '@/api/video/config'
import { ElMessage } from 'element-plus'

export const useConfigStore = defineStore('config', () => {
    const defaultConfigs = ref([])
    const multiConfigs = ref([])
    const configs = ref([])
    const total = ref(0)
    const loading = ref(false)

    // 获取配置列表
    const fetchConfigs = async (query) => {
        try {
            loading.value = true
            const { data } = await getConfigList(query)
            configs.value = data.records
            total.value = data.total
        } catch (error) {
            ElMessage.error(error.message || '获取配置列表失败')
        } finally {
            loading.value = false
        }
    }

    // 保存配置
    const saveVideoConfig = async (configData) => {
        try {
            loading.value = true
            await saveConfig(configData)
        } catch (error) {
            ElMessage.error(error.message || '保存配置失败')
            throw error
        } finally {
            loading.value = false
        }
    }

    // 保存多数字人配置
    const saveMultiAvatarConfig = async (config) => {
        try {
            await saveMultiConfig(config)
        } catch (error) {
            throw new Error(error.response?.data?.message || '保存多数字人配置失败')
        }
    }

    // 获取多数字人配置详情
    const getMultiAvatarConfig = async (id) => {
        try {
            const { data } = await fetchMultiAvatarConfig(id)
            return data
        } catch (error) {
            throw new Error(error.response?.data?.message || '获取多数字人配置详情失败')
        }
    }

    // 设置默认配置
    const setDefault = async (id) => {
        try {
            await setDefaultConfig(id)
            ElMessage.success('设置成功')
        } catch (error) {
            ElMessage.error(error.message || '设置默认配置失败')
        }
    }

    const setCaption = async (id) => {
        try {
            await setCaptionConfig(id)
            ElMessage.success('设置成功')
        } catch (error) {
            ElMessage.error(error.message || '设置默认配置失败')
        }
    }

    const getDefault = async () => {
        try {
            const { data } = await getDefaultConfig()
            defaultConfigs.value = data
        } catch (error) {
            ElMessage.error(error.message || '获取默认配置失败')
        }
    }

    const getMulti = async () => {
        try {
            const { data } = await getMultiConfig()
            multiConfigs.value = data
        } catch (error) {
            ElMessage.error(error.message || '获取默认配置失败')
        }
    }

    // 删除配置
    const removeConfig = async (id) => {
        try {
            await deleteConfig(id)
            ElMessage.success('删除成功')
        } catch (error) {
            ElMessage.error(error.message || '删除配置失败')
        }
    }

    // 删除多数字人配置
    const removeMultiAvatarConfig = async (id) => {
        try {
            await removeMultiConfig(id)
        } catch (error) {
            throw new Error(error.response?.data?.message || '删除多数字人配置失败')
        }
    }

    // 批量删除文章
    const batchDelete = async (ids) => {
        try {
            await batchDeleteConfigs(ids)
            ElMessage.success('批量删除成功')
        } catch (error) {
            ElMessage.error(error.message || '批量删除失败')
            throw error
        }
    }

    const syncAvatars = async () => {
        try {
            const response = await syncKeyAvatars()
            return response.data
        } catch (error) {
            throw new Error(error.response?.data?.message || '同步失败')
        }
    }

    const getLastSyncInfo = async () => {
        try {
            const response = await getLastSyncAvatarInfo()
            return response.data
        } catch (error) {
            console.error('Failed to get sync info:', error)
            return null
        }
    }

    return {
        configs,
        loading,
        total,
        defaultConfigs,
        multiConfigs,
        fetchConfigs,
        saveVideoConfig,
        setDefault,
        setCaption,
        removeConfig,
        getDefault,
        batchDelete,
        syncAvatars,
        getLastSyncInfo,
        saveMultiAvatarConfig,
        getMultiAvatarConfig,
        removeMultiAvatarConfig,
        getMulti
    }
})